@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.UserStore;
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
@inject IDispatcher dispatcher
@inject IState<UpdateUserState> updateUserState

<RadzenTemplateForm Submit=@AddUser TItem="AddUserForm" Data=@addUser>
    @if (!updateUserState.Value.IsUpdating && !string.IsNullOrWhiteSpace(updateUserState.Value.ErrorMessage))
    {
        <RadzenAlert AllowClose="false" AlertStyle="AlertStyle.Danger" ShowIcon="true" Variant="Variant.Flat" Shade="Shade.Lighter">@updateUserState.Value.ErrorMessage</RadzenAlert>
    }

    <!-- Name -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Name</RadzenText>
        <RadzenTextBox Name="Name" @bind-Value="@addUser.Name" Class="w-100"></RadzenTextBox>
        <RadzenRequiredValidator Component="Name" Text="@Global.NameIsRequired"></RadzenRequiredValidator>
    </div>
    <!-- FirstName -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Firstname</RadzenText>
        <RadzenTextBox Name="FirstName" @bind-Value="@addUser.FirstName" class="w-100"></RadzenTextBox>
    </div>
    <!-- LastName -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Lastname</RadzenText>
        <RadzenTextBox Name="LastName" @bind-Value="@addUser.LastName" class="w-100"></RadzenTextBox>
    </div>
    <!-- Middlename -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Middlename</RadzenText>
        <RadzenTextBox Name="Middlename" @bind-Value="@addUser.Middlename" class="w-100"></RadzenTextBox>
    </div>
    <!-- Email -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Email</RadzenText>
        <RadzenTextBox Name="Email" @bind-Value="@addUser.Email" class="w-100"></RadzenTextBox>
        <RadzenEmailValidator Component="Email" Text="@Global.EmailIsInvalid" />
    </div>
    <RadzenButton class="mt-1" Variant="Variant.Flat" ButtonType="ButtonType.Submit" ButtonStyle="ButtonStyle.Success" Text="@(updateUserState.Value.IsUpdating? Global.Adding: Global.Add)" Disabled=updateUserState.Value.IsUpdating />
</RadzenTemplateForm>

@code {
    record AddUserForm
    {
        public string Name { get; set; }
        public string? FirstName { get; set; } = null;
        public string? LastName { get; set; } = null;
        public string? Middlename { get; set; } = null;
        public string? Email { get; set; } = null;
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        dispatcher.Dispatch(new StartAddUserAction());
    }

    AddUserForm addUser = new AddUserForm();

    void AddUser(AddUserForm user)
    {
        dispatcher.Dispatch(new AddUserAction
        {
            Email = user.Email,
            Firstname = user.FirstName,
            Lastname = user.LastName,
            Middlename = user.Middlename,
            Name = user.Name
        });
    }
}
